package com.school.sports.util;

import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

/**
 * 密码测试工具类
 * 用于测试密码加密和验证功能
 *
 * @author System
 * @since 2025-10-13
 */
public class PasswordTestUtil {

    private static final BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();

    /**
     * 测试密码加密和验证
     *
     * @param args 命令行参数
     */
    public static void main(String[] args) {
        String rawPassword = "123456";

        System.out.println("=== 密码测试工具 ===");
        System.out.println("原始密码: " + rawPassword);
        System.out.println();

        // 生成新的密码哈希
        String encodedPassword = encoder.encode(rawPassword);
        System.out.println("生成的密码哈希: " + encodedPassword);

        // 验证生成的哈希
        boolean matches = encoder.matches(rawPassword, encodedPassword);
        System.out.println("密码验证结果: " + matches);
        System.out.println();

        // 测试已知的哈希值
        String[] testHashes = {
            "$2a$10$QBtNs4WT18sP68EYn0lr/uEdePF/eZyV9qDoC508.OcKcyfevQJAW",
            "$2a$10$va6uW/a4s2m/j3aUkX5cSueLIGW9fT7/SFtIi3QwOFhHht5jcWH22"
        };

        System.out.println("=== 测试已知哈希值 ===");
        for (int i = 0; i < testHashes.length; i++) {
            String hash = testHashes[i];
            boolean testMatches = encoder.matches(rawPassword, hash);
            System.out.println("哈希 " + (i + 1) + ": " + testMatches);
            System.out.println("值: " + hash);
        }
    }

    /**
     * 生成密码哈希
     *
     * @param rawPassword 原始密码
     * @return 加密后的密码哈希
     */
    public static String encodePassword(String rawPassword) {
        return encoder.encode(rawPassword);
    }

    /**
     * 验证密码
     *
     * @param rawPassword 原始密码
     * @param encodedPassword 加密后的密码
     * @return 验证结果
     */
    public static boolean matches(String rawPassword, String encodedPassword) {
        return encoder.matches(rawPassword, encodedPassword);
    }
}